Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select and Text are not fused iterators #184

Merged
merged 2 commits into from
Jul 16, 2024

Conversation

Noname-Official
Copy link
Contributor

use scraper::{Html, Selector};

const HTML: &str = r"
<div>
    <p>Hello</p>
    <p>World</p>
</div>
";

let html = Html::parse_fragment(HTML);
let selector = Selector::parse("div").unwrap();
let div = html.select(&selector).next().unwrap();
let selector = Selector::parse("p").unwrap();
let mut elements = div.select(&selector);
assert!(elements.next().is_some());
assert!(elements.next().is_some());
assert!(elements.next().is_none());
assert!(elements.next().is_some());

@adamreichold
Copy link
Member

adamreichold commented Jul 6, 2024

Sorry for introducing this bug and thank you for fixing it! I think I made the wrong assumption here that Traverse would be fused, but it reuses its initial state as its final state and therefore just restarts iterating the subtree after completing it once.

I do think it would be better to fix Traverse, e.g. make it fused by giving it a root: Option<NodeRef<'a, T>> field that is set to None once iteration is complete to differentiate its initial and final state (without increasing its size). But I fear, if we ever want to make changes to ego-tree, we'll have to fork it as the author seems unresponsive, so this certainly is the better short-term fix.

@cfvescovo
Copy link
Member

Merging this as a temporary fix

@cfvescovo cfvescovo merged commit 37b062e into rust-scraper:master Jul 16, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants